package com.linkgie.galaxyframework.httpproxy.converters;

import com.linkgie.galaxyframework.httpproxy.ResponseHandler;
import com.linkgie.galaxyframework.httpproxy.ServiceInvocationContext;
import com.linkgie.galaxyframework.httpproxy.ServiceRequest;
import com.linkgie.galaxyframework.httpproxy.ServiceResponse;

/**
 * 默认的响应处理器；
 * <p>
 * 
 */
public class DefaultResponseHandler implements ResponseHandler {

    private ResponseHandler successHandler;

    private ResponseHandler errorHandler;

    public DefaultResponseHandler(ResponseHandler successHandler, ResponseHandler errorHandler) {
        this.successHandler = successHandler;
        this.errorHandler = errorHandler;
    }

    @Override
    public Object handleResponse(ServiceRequest request, ServiceResponse response,
            ServiceInvocationContext invocationContext) throws Exception {

        if (response.getHttpStatusCode() >= 300) {
            return errorHandler.handleResponse(request, response, invocationContext);
        }
        return successHandler.handleResponse(request, response, invocationContext);
    }

}
